package com.weirdor.wchat.admin.web;

import com.weirdor.wchat.admin.web.base.AbstractController;
import com.weirdor.wchat.common.utils.R;
import com.weirdor.wchat.module.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author weirdor
 * @since 2018-03-05
 */
@RestController
@RequestMapping(value = "/sysUsers")
public class SysUserController  extends AbstractController{


    private static final Logger _logger = LoggerFactory.getLogger(SysUserController.class);

    // 创建线程安全的Map
    private static Map<Long, SysUser> users = Collections.synchronizedMap(new HashMap<Long, SysUser>());

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public R getUserList() {

        // 处理"/users/"的GET请求，用来获取用户列表
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        List<SysUser> r = new ArrayList<>(users.values());
        return R.ok().put("page", r);
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public R postUser(@ModelAttribute SysUser user) {
        // 处理"/users/"的POST请求，用来创建User
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        users.put(user.getUserId(), user);
        return  R.ok("新增成功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R getUser(@PathVariable Long id) {
        // 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        return R.ok("查询成功").put("userId",users.get(id));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public R putUser(@PathVariable Long id, @ModelAttribute SysUser user) {
        // 处理"/users/{id}"的PUT请求，用来更新User信息
        SysUser u = users.get(id);
        u.setUsername(user.getUsername());
        u.setPassword(user.getPassword());
        users.put(id, u);
        return R.ok( "更新成功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R deleteUser(@PathVariable Long id) {
        // 处理"/users/{id}"的DELETE请求，用来删除User
        users.remove(id);
        return R.ok( "删除成功");
    }
}

